layui.config({
    base: '/system/'
}).extend({
    treetable: 'treetable-lay/treetable'
}).use(['form','layer','table','laytpl','treetable'],function(){
    var form = layui.form,
        layer = layui.layer,
        $ = layui.jquery,
        table = layui.table;

    var tableIns = layui.treetable.render({
    	treeColIndex: 1,
        treeSpid: 0,
        treeIdName: 'id',
        treePidName: 'pid',
        elem: '#resourceList',
        url : '/sysResource/list',
        cellMinWidth : 95,
        page : true,
        height : "full-125",
        limits : [10,15,20,25],
        limit : 20,
        id : "resourceListTable",
        cols : [[
        	{type:'numbers'},
            {field: 'name', title: '资源名称', minWidth:100, align:"center"},
            {field: 'url', title: '路径', minWidth:100, align:"left"},
            {field: 'descr', title: '描述', minWidth:100, align:"center"},
            {field: 'isQuick', title: '是否快捷入口', minWidth:100, align:"center",templet:function(d){if(d.isQuick==1){return "是"}else{return "否"}}},
            {field: 'bg_color', title: '背景色', minWidth:90, align:"center"},
            {title: '操作', minWidth:175, templet:'#bar',fixed:"right",align:"center"}
        ]]
    });
    //列表操作
    table.on('tool(resourceList)', function(obj){
    var layEvent = obj.event,
        data = obj.data;

    if(layEvent === 'tableUpdate'){ //编辑
        top.layer.open({
            title :"编辑菜单",
            type : 2,
            area : ["500px","550px"],
            content : '/sysResource/toEditResource?id='+data.id,
            zIndex: 999,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                setTimeout(function(){
                    layui.layer.tips('点击此处返回菜单列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            },
            end: function(){
                table.reload('resourceListTable');
            }
        });
    }else if(layEvent === 'grant'){ //启用禁用
        var index = layer.open({
            title : "菜单授权",
            type : 2,
            area : ["500px","550px"],
            content : "/sysResource/toGrant?id="+data.id,
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                body.find(".name").val(data.name);
                body.find(".id").val(data.id);
                form.render();
                setTimeout(function(){
                    layui.layer.tips('点击此处返回菜单列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        });
        window.sessionStorage.setItem("index",index);
    }else if(layEvent === 'tableDelete'){ //删除
        layer.confirm('确定删除此菜单？',{icon:3, title:'提示信息'},function(index){
            $.ajax({
                url:"/sysResource/toResourceList",
                type:"post",
                dataType:"json",
                data:{
                    pid:data.id
                },
                success:function(res){
                    if(res.length===0){
                        $.ajax({
                            url: "/sysResource/deleteResource?id="+data.id,
                            type:"post",
                            dataType:"json",
                            data:{
                                ids :data.id //登录名
                            },
                            success:function(res){
                                if(res.code === 0){
                                    table.reload();
                                    top.layer.msg("操作成功！");
                                    setTimeout(function(){
                                        window.location.reload();
                                    },1000);
                                }else{
                                    layer.close(index);
                                    top.layer.msg("操作失败！");
                                }
                            }
                        });
                    }else{
                        layer.close(index);
                        layer.msg("此菜单下有子菜单，无法删除！");
                    }
                }
            });
        });
    }
});
});
function insert() {
    layer.open({
        type : 2,
        title : '添加菜单',
        content : '/sysResource/toAddResource',
        shade : 0.5,
        area : ["500px","550px"],
        maxmin : true
    });
}

